package com.example.himalaya2.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya2.R;
import com.example.himalaya2.adapters.PlayListAdapter;
import com.example.himalaya2.base.BaseApplication;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;

import static com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl.PlayMode;


public class SobPopWindow extends PopupWindow {

    private final View mView;
    private TextView mPlayListCloseBtn;
    private RecyclerView mTrackList;
    private PlayListAdapter mPlayListAdapter;
    private TextView mPlayModeTv;
    private ImageView mPlayModeIv;
    private View mPlayModeContainer;
    private PlayListPlayModeClickListener mPlayModeClickListener = null;
    private View mOderContainer;
    private ImageView mOrderIcon;
    private TextView mOrderText;

    public SobPopWindow() {
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.pop_animation);
        setOutsideTouchable(true);

        mView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list,null);
        setContentView(mView);
        initView();
        initEven();
    }

    private void initEven() {
        mPlayListCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mPlayModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPlayModeClickListener != null) {
                    mPlayModeClickListener.onPlayModeClick();
                }
            }
        });

        mOderContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPlayModeClickListener.onOrderClick();
            }
        });
    }
    /*
    * 更新播放顺序逆序的图标和文字
    * */
    public void updateOrderIcon(boolean isOrder) {
        mOrderIcon.setImageResource(isOrder?R.drawable.selector_play_mode_list_order:R.drawable.selector_play_mode_list_revers);
        mOrderText.setText(isOrder?BaseApplication.getAppContext().getResources().getString(R.string.order_text):
                BaseApplication.getAppContext().getResources().getString(R.string.rever_text));
    }

    private void initView() {
        mPlayListCloseBtn = mView.findViewById(R.id.play_list_close_btn);
        mTrackList = mView.findViewById(R.id.play_list_rv);
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.getAppContext());
        mTrackList.setLayoutManager(layoutManager);
        //设置适配器
        mPlayListAdapter = new PlayListAdapter();
        mTrackList.setAdapter(mPlayListAdapter);
        //  play mode
        mPlayModeTv = mView.findViewById(R.id.play_list_play_mode_tv);
        mPlayModeIv = mView.findViewById(R.id.play_list_play_mode_iv);
        mPlayModeContainer = mView.findViewById(R.id.play_list_play_mode_container);
        // play order
        mOderContainer = mView.findViewById(R.id.play_list_oder_container);
        mOrderIcon = mView.findViewById(R.id.play_list_oder_iv);
        mOrderText = mView.findViewById(R.id.play_list_oder_tv);
    }
    /*
    * 给适配器设置数据
    * */
    public void setListData(List<Track> tracks){
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setData(tracks);
        }
    }

    public void setCurrentPlayPosition(int position) {
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setSetCurrentPlayPosition(position);
            mTrackList.scrollToPosition(position);
        }
    }

    public void setPlayListItemClickedListener(PlayListItemClickedListener listener) {
        mPlayListAdapter.setOnItemClickListener(listener);
    }

    // 更新播放列表的 播放模式
    public void upDatePlayMode(XmPlayListControl.PlayMode currentMode) {
        upDatePlayModeBtnImage(currentMode);
    }


    private void upDatePlayModeBtnImage(XmPlayListControl.PlayMode playMode) {
        int resId = R.drawable.selector_play_mode_list_order;
        int textId = R.string.play_mode_oder_text;
        switch (playMode) {
            case PLAY_MODEL_LIST:
                resId = R.drawable.selector_play_mode_list_order;
                textId = R.string.play_mode_oder_text;
                break;
            case PLAY_MODEL_RANDOM:
                resId = R.drawable.selector_paly_mode_random;
                textId = R.string.play_mode_random_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resId = R.drawable.selector_paly_mode_single_loop;
                textId = R.string.play_mode_single_loop_text;
                break;
            case PLAY_MODEL_LIST_LOOP:
                resId = R.drawable.selector_paly_mode_list_order_looper;
                textId = R.string.play_mode_list_loop_text;
                break;
            default:
                resId = R.drawable.selector_play_mode_list_order;
        }
        mPlayModeIv.setImageResource(resId);
        mPlayModeTv.setText(textId);
    }

    public interface PlayListItemClickedListener {
        void onItemClicked(int position);
    }

    public void setPlayListPlayModeClickListener(PlayListPlayModeClickListener listener) {
        mPlayModeClickListener = listener;
    }
    public interface PlayListPlayModeClickListener {
        // 播放模式被点击
        void onPlayModeClick();
        // 播放顺序被点击
        void onOrderClick();
    }
}
